/**
 * AStore.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.store;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import extjsdyntran.translation.ITranslationService;


public abstract class AStore implements IStore {

	final protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public  AStore(HashMap<String,String> params) throws Exception {
		
	}
	

	@Override
	public boolean load(ITranslationService service,String [] lg) {
		
		Hashtable<String, Properties> languages = service.getAllTranslations();
		for (int i = 0; i < lg.length; i++) {
			if (lg[i].startsWith("AUTO"))
				continue;

			if (!languages.containsKey(lg[i])) {				
					try {
						loadLanguage(service,lg[i]);
					} catch (Exception e) {
						logger.error("Error loading languages from store...",e);
					}	
				
			}
		}
		return true;
	}

	@Override
	public boolean save(ITranslationService service,String language) {
		
		Hashtable<String, Properties> languages = service.getAllTranslations();
		
		if ("*".equals(language)) {
			
			Enumeration<String> e = languages.keys();

			try {
				while (e.hasMoreElements()) {
					String s = e.nextElement();
					Properties p = languages.get(s);
					saveLanguage(service,p,s);
				}
				return true;
			} catch (Exception e1) {
				logger.error("Error saving languages to store...", e1);
				return false;
			}

		} else {
			try {
				Properties p = languages.get(language);
				if (p == null) {
					logger.warn("Trying to save unsupported language {} to store by service {}", language,service.getClass().getName());
					return false;
				}
				saveLanguage(service,p,language);
				return true;
			} catch (Exception e1) {
				logger.error("Error saving language {} to store...",language, e1);
				return false;
			}

		} // end else

	}
}
